<?php

abstract class Application_Model_Abstract {
    
    protected $_dbTable;
    
    public function fetchAll($where = null, $order = null, $count = null, $offset = null) {        
        return $this->_dbTable->fetchAll($where, $order, $count, $offset);
    }
    
    public function find($id){
        return $this->_dbTable->find((int)$id)->current();
    }
    
    public function save(array $data, $where = null) {
        
        $dt = new Zend_Date();
        $dt = $dt->toString("yyyy-MM-dd HH:mm:s");
                
        if (isset($data['id'])) {
            $data['updated_at'] = $dt;
            return $this->_update($data, $where);
        } else {
            $data['created_at'] = $dt;
            return $this->_insert($data);
        }
    }
    
    protected function _insert(array $data) {
        return $this->_dbTable->insert($data);
    }
    
    protected function _update(array $data, $where) {
        return $this->_dbTable->update($data, $where);
    }
    
    public function __get($key) {
        if(property_exists($this, $key)) {
            $method = 'get'.ucfirst($key);
            if (method_exists($this, $method)) {
                return $this->$method();
            } else {
                return $this->$key;
            }
        }
        return null;
    }

    public function __set($key, $value) {
        if(property_exists($this, $key)) {
            $method = 'set'.ucfirst($key);
            if (method_exists($this, $method)) {
                $this->$method($value);
            } else {
                $this->$key = $value;
            }
        }
    }
    
}
